package academic.ui.client.view;



import java.util.ArrayList;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewImpl;

import ui.client.GenericClickable;
import ui.client.GenericContainerTag;

import academic.ui.client.config.CSSConstants;
import academic.ui.client.presenter.GamePresenter;
import academic.ui.client.widgets.ProductWidget;
import academic.ui.client.widgets.QuickFlipWidget;
import ecommerce.soco.shared.GWT_Product;

public class GameView extends ViewImpl implements GamePresenter.MyView {
	
	public GenericContainerTag div = new GenericContainerTag("div");
	
	private GenericClickable signInLink = new GenericClickable("a","sign in");
	private GenericClickable myAccountLink = new GenericClickable("a","my account");
	private GenericClickable newGame = new GenericClickable("a","deals");
	private GenericClickable howItWorks = new GenericClickable("a","how this works");

	private ArrayList<QuickFlipWidget> quickFlipArray = new ArrayList<QuickFlipWidget>();
	
	private ArrayList<HasClickHandlers> dealHandlers = new ArrayList<HasClickHandlers>();
	private ArrayList<HasClickHandlers> buyNowHandlers = new ArrayList<HasClickHandlers>();
	
	private ArrayList<ProductWidget> prodArray = new ArrayList<ProductWidget>();
	
	private GenericContainerTag gameSlot = new GenericContainerTag("div");
	private GenericContainerTag overlay = new GenericContainerTag("div");
	GenericContainerTag signInDiv = new GenericContainerTag("div");
	
	@Inject
	public GameView(CSSConstants css) {
		div.setStyleName("game");
		
		signInLink.getElement().setId("signInFlipLink");
		signInLink.getElement().setAttribute("rel","#signInModal");
		overlay.setStyleName("overlay");
		overlay.getElement().setId("signInModal");
		
		signInDiv.add(signInLink);
		signInDiv.add(overlay);
	
		QuickFlipWidget qf = new QuickFlipWidget("quickFlip");
		QuickFlipWidget qf1 = new QuickFlipWidget("quickFlip2");
		QuickFlipWidget qf2 = new QuickFlipWidget("quickFlip3");
		
		qf.updateFront(signInDiv);
		qf1.updateFront(newGame);
		qf2.updateFront(howItWorks);
		quickFlipArray.add(qf);quickFlipArray.add(qf1);quickFlipArray.add(qf2);
		
		
		gameSlot.setStyleName("gamePresenterSlot");
	}
	
	@Override
	public Widget asWidget() {
		return div;		
	}

	@Override
	public void setup() {
		div.clear();
		
		for(QuickFlipWidget qfWidge : quickFlipArray)
		{
			div.add(qfWidge);
		}
		div.add(gameSlot);
		
	}

	@Override
	public HasClickHandlers signInLink() {
		return signInLink;
	}
	
	@Override
	public HasClickHandlers newGame() {
		return newGame;
	}

	@Override
	public HasClickHandlers myAccount() {
		return myAccountLink;
	}

	@Override
	public ArrayList<HasClickHandlers> getDealHandlers() {
		return dealHandlers;
	}
	
	@Override
	public ArrayList<HasClickHandlers> buyNowHandlers() {
		return buyNowHandlers;
	}
	
	@Override
	public void startGame(ArrayList<GWT_Product> products) {
		int i=0;
		dealHandlers.clear();
		prodArray.clear();
		buyNowHandlers.clear();
		for(GWT_Product product : products)
		{
			ProductWidget prodWidge = new ProductWidget();
			prodWidge.setProduct(product);
			prodArray.add(prodWidge);
			dealHandlers.add(prodWidge.productClick());
			buyNowHandlers.add(prodWidge.buyNow());
			if(quickFlipArray.size()>i)
				quickFlipArray.get(i).updateBack(prodWidge);
			i++;
		}
		for(QuickFlipWidget qfw : quickFlipArray)
		{
			if(!qfw.hasBackWidget())
				DOM.setStyleAttribute(qfw.getElement(), "display", "none");
			if(qfw.hasBackWidget())
			{
				qfw.hideFront();
				qfw.flip(1);
			}
		}
		
	}
	
	@Override
	public void setInSlot(Object slot, Widget w) {
		Log.debug("setInSlot:"+w.getStyleName());
		if(slot==GamePresenter.slot) {
			gameSlot.clear();
			gameSlot.add(w);
		}
	}

	@Override
	public void reset() {
		for(final QuickFlipWidget qfWidge : quickFlipArray)
		{
			qfWidge.showDefault();
		}
	}



	@Override
	public void updateLoggedOut() {
		quickFlipArray.get(0).updateFront(signInDiv);
		quickFlipArray.get(1).updateFront(newGame);
		quickFlipArray.get(2).updateFront(howItWorks);
		reset();
	}

	@Override
	public void resetLoggedIn() {
		quickFlipArray.get(0).updateFront(myAccountLink);
	}

	@Override
	public void noActiveDeals() {
		if(quickFlipArray.get(1).isDefaultShowing()) {
			GenericContainerTag noActiveDealsDiv = new GenericContainerTag("div");
			noActiveDealsDiv.setStyleName("noActiveDealsDiv");
			GenericContainerTag noDealsParagraph = new GenericContainerTag("p","There are currently no active deals available.  Amazing deals arriving soon!");
			noActiveDealsDiv.add(noDealsParagraph);
			quickFlipArray.get(1).updateBack(noActiveDealsDiv);
			quickFlipArray.get(1).flip();
		}
	}
	

}
